package cn.didi.feidi.androidorder.core;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;

import cn.didi.feidi.androidorder.api.ApiRequest;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.api.ErrorEvent;
import cn.didi.feidi.androidorder.api.HttpEngine;


/**
 * Created by king on 2017/7/16.
 */

public class AppActionRequest<T> implements Runnable {

    private final static int ACTION_CALL_BACK_SUCCESS = 0;
    private final static int ACTION_CALL_BACK_FAILURE = 1;

    private ApiRequest apiRequest;

    private ActionCallbackListener<T> listener;

    private Type typeOfT;

    private HttpEngine httpEngine;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
                case ACTION_CALL_BACK_SUCCESS:
                    ApiResponse<T> response = (ApiResponse<T>) msg.obj;

//                    String result2="{\"event\":\"0\",\"msg\":\"success\",\"obj\":null,\"objList\":{\"password\":\"\",\"phoneNumber\":\"\",\"uid\":\"135604191262017-07-18 07:55:35.47\",\"token\":\"4BF4441E7ED923B78327DFA39DE385BF\",\"facialFeature\":\"\",\"facialUrl\":\"\",\"imei\":\"\",\"successSign\":\"\"},\"currentPage\":0,\"pageSize\":0,\"maxCount\":0,\"maxPage\":0,\"success\":true}";
//                    ApiResponse<T> response = HttpEngine.getInstance().getResponseObject(result2, new TypeToken<ApiResponse<T>>() {
//                    }.getType());
                    if (listener != null && response != null) {

                        if (response.isSuccess()) {
                            listener.onSuccess(response.getMsg(), response.getObj());
                        } else {
                            listener.onFailure(response.getEvent(),
                                    response.getMsg());
                        }
                    } else {
                        listener.onFailure(ErrorEvent.PARAM_ILLEGAL, "响应为空");
                    }
                    break;
                case ACTION_CALL_BACK_FAILURE:
                    Exception ex = (Exception) msg.obj;
                    if (listener != null)
                        listener.onFailure(ErrorEvent.PARAM_ACCESS_FAILURE, ex.getMessage());
                    break;
                default:
                    break;
            }
        }
    };


    public AppActionRequest(String requestURL, Map<String, String> data, Type typeOfT, final ActionCallbackListener<T> actionCallbackListener) {
        this.apiRequest = new ApiRequest(requestURL, data);
        this.listener = actionCallbackListener;

        this.httpEngine = HttpEngine.getInstance();
        this.typeOfT = typeOfT;
    }


    @Override
    public void run() {
        Message msg = mHandler.obtainMessage();
        try {
            ApiResponse<T> result = httpEngine.postHandle(apiRequest.getRequestURL(), apiRequest.getParamMap(), this.typeOfT);

            msg.obj = result;
            msg.arg1 = ACTION_CALL_BACK_SUCCESS;
        } catch (IOException e) {
            e.printStackTrace();
            msg.obj = e;
            msg.arg1 = ACTION_CALL_BACK_FAILURE;
        } finally {
            mHandler.sendMessage(msg);
        }
    }
}
